package com.ld.security.server.user.modules.dsp.web;


import com.ld.security.common.mybatis.base.po.EntityPo;
import com.ld.security.common.mybatis.base.po.IdPo;
import com.ld.security.common.mybatis.base.vo.DataVo;
import com.ld.security.common.mybatis.base.vo.ListVo;
import com.ld.security.common.tomcat.base.api.SuperApi;
import com.ld.security.server.user.modules.dsp.entity.DspDispatchEntity;
import com.ld.security.server.user.modules.dsp.entity.DspLogEntity;
import com.ld.security.server.user.modules.dsp.po.CodePo;
import com.ld.security.server.user.modules.dsp.service.IDspDispatchService;
import com.ld.security.server.user.modules.dsp.service.IDspLogService;
import com.ld.security.server.user.modules.dsp.vo.ActDipatchVo;
import com.ld.security.server.user.modules.dsp.vo.DspLogSavePo;
import com.ld.security.server.user.modules.ups.util.UpsUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.concurrent.CompletableFuture;


@RestController
@Api(tags="广告调度Web管理")
@RequestMapping("/web/dspDispatch")
public class DspDispatchWeb {


        @Autowired
        IDspDispatchService iDspDispatchService;

        @Autowired
        IDspLogService iDspLogService;

        private void saveLog(ActDipatchVo vo ,String logType){

                DspLogEntity log = new DspLogEntity();
                log.setLogType(logType);
                log.setAdvertId(vo.getAdvertId());
                log.setSpaceId(vo.getSpaceId());
                log.setUpsUserId(UpsUtil.getUpsUserId());

                CompletableFuture.runAsync(() -> {
                        iDspLogService.save(log);
                });
        }

        @ApiOperation("广告日志保存")
        @RequestMapping(value = "/saveDspLog",method = RequestMethod.POST)
        public DataVo DspLogSavePo( @Valid @RequestBody DspLogSavePo po){
                this.saveLog(po.getEntity(),po.getLogType());
                return DataVo.of();
        }




        @ApiOperation("获得广告调度")
        @RequestMapping(value = "/getActDipatch",method = RequestMethod.POST)
        public DataVo getActDipatch( @Valid @RequestBody CodePo po){
                List<ActDipatchVo> vo =  iDspDispatchService.getActDipatch(po.getCode());
                return DataVo.of( ListVo.of(vo));
        }


}
